<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Excel;
use App\Models\Score;
use App\Models\UserScore;

class ExcelController extends Controller
{
    public function year_scores_all(Request $request)
    {
        Excel::create('年度所有人评分统计信息',function ($excel) use ($request){
            $excel->sheet('score', function ($sheet) use ($request){

                $map['year_id'] = $request->id;

                $score = UserScore::with('user','to_user')->where($map);

                if($request->name) {
                    $score->whereHas('user', function ($q) {
                        $q->where('name', 'like' , '%' .  request('name') . '%');
                    });
                }

                $count = $score->count();

                $all_num = $score->avg("value");

                $scores = $score->orderBy('id','desc')->get();

                $sheet->appendRow(['ID',"评分人姓名",'评分人部门','评分人职位','评分人角色','评分',"被评分人姓名",'被评分人部门','被评分人职位','被评分人角色', "平均分(".intval($all_num).")","共(".$count.")人"]);

                $id = 1;
                foreach ($scores as $v)
                {
                    $sheet->appendRow([$id++ ,$v->user['name'],$v->user['department_name'],$v->user['post_name'],$v->user['role_name'],$v['value'],$v->to_user['name'],$v->to_user['department_name'],$v->to_user['post_name'],$v->to_user['role_name']]);
                }
            });
        })->export('xls');
    }

    public function year_scores_export(Request $request)
    {
        Excel::create('年度评分统计信息',function ($excel) use ($request){
            $excel->sheet('score', function ($sheet) use ($request){

               $map['year_id'] = $request->id;

                $score = Score::with('user')->where($map);

                if($request->name) {
                    $score->whereHas('user', function ($q) {
                        $q->where('name', 'like' , '%' .  request('name') . '%');
                    });
                }

                $count = $score->count();

                $all_num = $score->avg("value");

                $scores = $score->orderBy('id','desc')->get();

                $sheet->appendRow(['ID',"姓名",'部门','职位','评分', '评分等级', "平均分(".intval($all_num).")","共(".$count.")人"]);

                foreach ($scores as $v)
                {
                    $sheet->appendRow([$v['id'] ,$v->user['name'],$v->user['department_name'],$v->user['post_name'],$v['value'],$v['level']]);
                }
            });
        })->export('xls');
    }

    public function user_scores_export(Request $request)
    {
        Excel::create('用户评分统计信息',function ($excel) use ($request){
            $excel->sheet('score', function ($sheet) use ($request){

                $map['user_id'] = $request->user_id;
                $request->id && $map['year_id'] = $request->id;

                $score = Score::with('user')->where($map);

                $all_num = $score->avg("value");

                $scores = $score->orderBy('id','desc')->get();

                $sheet->appendRow(['ID',"姓名",'部门','职位','评分', '评分等级', '年度', "平均分(".intval($all_num).")"]);

                foreach ($scores as $v)
                {
                    $sheet->appendRow([$v['id'] ,$v->user['name'],$v->user['department_name'],$v->user['post_name'],$v['value'],$v['level'],$v['year_number']]);
                }
            });
        })->export('xls');
    }
}
